<?php

include_once("include/include.php.inc");
html_header("Support admin");



if(isset($_POST['action'])){
	switch($_POST['action']){
		case 'modify':
			unset($_POST['action']);
			foreach($_POST as $ukey => $permlist){
				if($ukey{0} == "u"){
					$perm = 0;
					foreach($permlist as $p) $perm |= intval($p);
					license_change(substr($ukey,1), $perm);
				}
			}
		break;
		case 'add':
			unset($_POST['action']);
			license_add($_POST['pname'], $_POST['pdesc']);
		break;
		case 'delete':
			unset($_POST['action']);
			license_del($_POST['pname']);
		break;
	}	
}

$perms = license_get_list();
$count = 0;



$table = '<form action="'.$_SERVER['PHP_SELF'].'" method="post">
<table width="100%" bgcolor=\"#336699\" BORDER=\"0\" bordercolor=\"#FFFFFF\">
	<td width="100"><b>User</b></td>';
foreach($perms as $perm){
	$table .= '<td><a title="'.$perm['description'].'"><b>'.$perm['name'].'</b></a></td>';
	unset($perm['name']);
}
$table .= '</tr>';
foreach(get_users_list() as $user){
	$table .= '<tr>';
	$table .= '<td>'.$user['username'].'</td>';
	foreach($perms as $perm){
		$name = 'u'.$user['id'].'[]';
		$checked = ((intval($user['permissions']) & intval($perm['id'])) ? 'checked': '');
		$table .= '<td><input type="checkbox" name="'.$name.'" value="'.$perm['id'].'" '.$checked.'></td>';
	}
	$table .= '</tr>';
	$count++;
}

$table .= '<tr><td colspan="'.(count($perms)+1).'"><input type="submit" name="action" value="modify"
></td></tr></table></form>';

echo "<div class='box'>\n"; 
echo " <div class='boxtop'><div></div></div>\n"; 
echo "  <div class='boxcontent'>\n"; 


echo $table;

echo "  </div>\n"; 
echo " <div class='boxbottom'><div></div></div>\n"; 
echo "</div>\n"; 
echo "\n";
echo "<br>";	
echo "<div class='box'>\n"; 
echo " <div class='boxtop'><div></div></div>\n"; 
echo "  <div class='boxcontent'>\n"; 

?>
<form action="<?=$_SERVER['PHP_SELF'];?>" method="post">
<h2>Add permission</h2>
<table border="0" cellpadding="10" bgcolor=\"#336699\" BORDER=\"0\" bordercolor=\"#FFFFFF\">
	<tr>
		<td><span>Perm Name</span></td>
	  <td><input name="pname" type="text" size="30" ></td>
		<tr><td>Description</td>
	  <td><input name="pdesc" type="text" size="30"></td></tr>
	  <td><input type="submit" name="action" value="add" ></td>
	  
	</tr>
</table>
</form>
<?php
echo "  </div>\n"; 
echo " <div class='boxbottom'><div></div></div>\n"; 
echo "</div>\n"; 
echo "\n";

echo "<br>";	

echo "<div class='box'>\n"; 
echo " <div class='boxtop'><div></div></div>\n"; 
echo "  <div class='boxcontent'>\n"; 
?>

<form action="<?=$_SERVER['PHP_SELF'];?>" method="post">
<h2>Delete permission</h2>
<table border="0" cellpadding="10" bgcolor=\"#336699\" BORDER=\"0\" bordercolor=\"#FFFFFF\">
	<tr>
		<td class="style3">Permission name</td>
	  <td><input name="pname" type="text" size="30" class="style3"></td>
	  <td><input type="submit" name="action" value="delete" class="style3"></td>
	  
	</tr>
</table>
</form>
<?php
echo "  </div>\n"; 
echo " <div class='boxbottom'><div></div></div>\n"; 
echo "</div>\n"; 
echo "\n";
html_footer();
?>

